package com.yanqu.road.dao.impl.mogulbirthday;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.mogulbirthday.MogulBirthdayRecordData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MogulBirthdayRecordDataDaoImpl extends TempDataObjectDao<MogulBirthdayRecordData> {


    public static String getMogulBirthdayRecordDataSql = "select * from `t_u_mogul_birthday_record` where `today` >=?";
    public Map<Long, MogulBirthdayRecordData> getMogulBirthdayRecordData(long today) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, MogulBirthdayRecordData> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getMogulBirthdayRecordDataSql);
            int i = 1;
            call.setLong(i++, today);
            rs = call.executeQuery();
            while (rs.next()) {
                MogulBirthdayRecordData data = parse(rs);
                result.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateMogulBirthdayRecordDataSql = "update `t_u_mogul_birthday_record` set getGiftNum=? where today=? and userId=?";
    @Override
    protected boolean updateData(MogulBirthdayRecordData mogulBirthdayRecordData) {
        mogulBirthdayRecordData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, mogulBirthdayRecordData.getGetGiftNum()));
        para.put(i++, new DbParameter(Types.BIGINT, mogulBirthdayRecordData.getToday()));
        para.put(i++, new DbParameter(Types.BIGINT, mogulBirthdayRecordData.getUserId()));
        if (getDb().executeNoneQuery(updateMogulBirthdayRecordDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertMogulBirthdayRecordDataSql = "insert into `t_u_mogul_birthday_record`(today,userId,getGiftNum) values (?,?,?)";
    @Override
    protected boolean addData(MogulBirthdayRecordData mogulBirthdayRecordData) {
        mogulBirthdayRecordData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, mogulBirthdayRecordData.getToday()));
        para.put(i++, new DbParameter(Types.BIGINT, mogulBirthdayRecordData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, mogulBirthdayRecordData.getGetGiftNum()));
        if (getDb().executeNoneQuery(insertMogulBirthdayRecordDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected MogulBirthdayRecordData parseData(ResultSet rs) throws SQLException {
        MogulBirthdayRecordData data = new MogulBirthdayRecordData();
        data.setToday(rs.getLong("today"));
        data.setUserId(rs.getLong("userId"));
        data.setGetGiftNum(rs.getLong("getGiftNum"));

        return data;
    }
}
